All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.swing.SwingUtilities

java.lang.Object
   |
   +----com.sun.java.swing.SwingUtilities

public class SwingUtilities
extends Object
implements SwingConstants
A collection of utility methods for Swing.


Constructor Index

 o SwingUtilities()

Method Index

 o computeDifference(Rectangle, Rectangle)
Convenience returning an array of rect representing the regions within rectA that do not overlap with rectB.
 o computeIntersection(int, int, int, int, Rectangle)
Convenience to calculate an intersection of two rectangles without allocating a new rectangle Return dest.
 o computeStringWidth(FontMetrics, String)
 o computeUnion(int, int, int, int, Rectangle)
Convenience to calculate the union of two rectangles without allocating a new rectangle Return dest
 o convertMouseEvent(Component, MouseEvent, Component)
Returns a MouseEvent similar to sourceEvent except that its x and y members have been converted to destination's coordinate system.
 o convertPoint(Component, int, int, Component)
Convert the point (x,y) in source coordinate system to destination coordinate system.
 o convertPoint(Component, Point, Component)
Convert a aPoint in source coordinate system to destination coordinate system.
 o convertPointFromScreen(Point, Component)
 o convertPointToScreen(Point, Component)
 o convertRectangle(Component, Rectangle, Component)
Convert the rectangle aRectangle in source coordinate system to destination coordinate system.
 o findFocusOwner(Component)
Return the child component which has focus, if any.
 o getAccessibleAt(Component, Point)
Returns the Accessible child contained at the local coordinate Point, if one exists.
 o getAccessibleChild(Component, int)
Return the nth Accessible child of the object.
 o getAccessibleChildrenCount(Component)
Returns the number of accessible children in the object.
 o getAccessibleIndexInParent(Component)
Get the index of this object in its accessible parent.
 o getAccessibleStateSet(Component)
Get the state of this object.
 o getAncestorNamed(String, Component)
Convience method for searching above comp in the component hierarchy and returns the first object of name it finds.
 o getAncestorOfClass(Class, Component)
Convience method for searching above comp in the component hierarchy and returns the first object of class c it finds.
 o getDeepestComponentAt(Component, int, int)
Returns the deepest child Component of parent that is at the location x, y.
 o getLocalBounds(Component)
Return the rectangle (0,0,bounds.width,bounds.height) for the component aComponent
 o getRoot(Component)
 o getRootPane(Component)
If c is a JRootPane descendant return its JRootPane ancestor.
 o invokeAndWait(Runnable)
Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.
 o invokeLater(Runnable)
Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread.
 o isDescendingFrom(Component, Component)
Return true if a component a descends from a component b
 o isEventDispatchThread()
 o isLeftMouseButton(MouseEvent)
 o isMiddleMouseButton(MouseEvent)
 o isRectangleContainingRectangle(Rectangle, Rectangle)
Return true if a contains b
 o isRightMouseButton(MouseEvent)
 o layoutCompoundLabel(FontMetrics, String, Icon, int, int, int, int, Rectangle, Rectangle, Rectangle, int)
Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string.
 o paintComponent(Graphics, Component, Container, int, int, int, int)
Paint a component c on an abitrary graphics g in the specified rectangle.
 o paintComponent(Graphics, Component, Container, Rectangle)
 o updateComponentTreeUI(Component)
A simple minded look and feel change: ask each node in the tree to updateUI(), i.e.
 o windowForComponent(Component)
Return aComponent's window

Constructors

 o SwingUtilities
 public SwingUtilities()

Methods

 o isRectangleContainingRectangle
 public static final boolean isRectangleContainingRectangle(Rectangle a,
                                                            Rectangle b)
Return true if a contains b

 o getLocalBounds
 public static Rectangle getLocalBounds(Component aComponent)
Return the rectangle (0,0,bounds.width,bounds.height) for the component aComponent

 o convertPoint
 public static Point convertPoint(Component source,
                                  Point aPoint,
                                  Component destination)
Convert a aPoint in source coordinate system to destination coordinate system. If source>is null,aPoint is assumed to be in destination's root component coordinate system. If destinationis null, aPoint will be converted to source's root component coordinate system. If both source and destination are null, return aPoint without any conversion.

 o convertPoint
 public static Point convertPoint(Component source,
                                  int x,
                                  int y,
                                  Component destination)
Convert the point (x,y) in source coordinate system to destination coordinate system. If source>is null,(x,y) is assumed to be in destination's root component coordinate system. If destinationis null, (x,y) will be converted to source's root component coordinate system. If both source and destination are null, return (x,y) without any conversion.

 o convertRectangle
 public static Rectangle convertRectangle(Component source,
                                          Rectangle aRectangle,
                                          Component destination)
Convert the rectangle aRectangle in source coordinate system to destination coordinate system. If source>is null,aRectangle is assumed to be in destination's root component coordinate system. If destinationis null, aRectangle will be converted to source's root component coordinate system. If both source and destination are null, return aRectangle without any conversion.

 o getAncestorOfClass
 public static Container getAncestorOfClass(Class c,
                                            Component comp)
Convience method for searching above comp in the component hierarchy and returns the first object of class c it finds. Can return null, if a class c cannot be found.

 o getAncestorNamed
 public static Container getAncestorNamed(String name,
                                          Component comp)
Convience method for searching above comp in the component hierarchy and returns the first object of name it finds. Can return null, if name cannot be found.

 o getDeepestComponentAt
 public static Component getDeepestComponentAt(Component parent,
                                               int x,
                                               int y)
Returns the deepest child Component of parent that is at the location x, y. If parent is not a Container, it is returned, otherwise this method is messaged again with the child component at x, y.

 o convertMouseEvent
 public static MouseEvent convertMouseEvent(Component source,
                                            MouseEvent sourceEvent,
                                            Component destination)
Returns a MouseEvent similar to sourceEvent except that its x and y members have been converted to destination's coordinate system. If source is null, sourceEvent x and y members are assumed to be into destination's root component coordinate system. If destination is null, the returned MouseEvent will be in source's coordinate system. sourceEvent will not be changed. A new event is returned. the source field of the returned event will be set to destination if destination is non null use the translateMouseEvent() method to translate a mouse event from one component to another without changing the source.

 o convertPointToScreen
 public static void convertPointToScreen(Point p,
                                         Component c)
 o convertPointFromScreen
 public static void convertPointFromScreen(Point p,
                                           Component c)
 o windowForComponent
 public static Window windowForComponent(Component aComponent)
Return aComponent's window

 o isDescendingFrom
 public static boolean isDescendingFrom(Component a,
                                        Component b)
Return true if a component a descends from a component b

 o computeIntersection
 public static Rectangle computeIntersection(int x,
                                             int y,
                                             int width,
                                             int height,
                                             Rectangle dest)
Convenience to calculate an intersection of two rectangles without allocating a new rectangle Return dest.

 o computeUnion
 public static Rectangle computeUnion(int x,
                                      int y,
                                      int width,
                                      int height,
                                      Rectangle dest)
Convenience to calculate the union of two rectangles without allocating a new rectangle Return dest

 o computeDifference
 public static Rectangle[] computeDifference(Rectangle rectA,
                                             Rectangle rectB)
Convenience returning an array of rect representing the regions within rectA that do not overlap with rectB. If the two Rects do not overlap, returns an empty array

 o isLeftMouseButton
 public static boolean isLeftMouseButton(MouseEvent anEvent)
 o isMiddleMouseButton
 public static boolean isMiddleMouseButton(MouseEvent anEvent)
 o isRightMouseButton
 public static boolean isRightMouseButton(MouseEvent anEvent)
 o computeStringWidth
 public static int computeStringWidth(FontMetrics fm,
                                      String str)
 o layoutCompoundLabel
 public static String layoutCompoundLabel(FontMetrics fm,
                                          String text,
                                          Icon icon,
                                          int verticalAlignment,
                                          int horizontalAlignment,
                                          int verticalTextPosition,
                                          int horizontalTextPosition,
                                          Rectangle viewR,
                                          Rectangle iconR,
                                          Rectangle textR,
                                          int textIconGap)
Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string. Locations are computed relative to the viewR rectangle.

 o paintComponent
 public static void paintComponent(Graphics g,
                                   Component c,
                                   Container p,
                                   int x,
                                   int y,
                                   int w,
                                   int h)
Paint a component c on an abitrary graphics g in the specified rectangle. The component is reparented to a private container (whose parent becomes p) which prevents c.validate() and and c.repaint() calls from propogating up the tree. The intermediate container has no other effect.

 o paintComponent
 public static void paintComponent(Graphics g,
                                   Component c,
                                   Container p,
                                   Rectangle r)
 o updateComponentTreeUI
 public static void updateComponentTreeUI(Component c)
A simple minded look and feel change: ask each node in the tree to updateUI(), i.e. to initialize its UI property with the current look and feel.

 o invokeLater
 public static void invokeLater(Runnable doRun)
Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeAndWait() calls queues the doHelloWorld Runnable for the event dispatching thread and then prints a message.
 Runnable doHelloWorld = new Runnable() {
     public void run() {
         System.out.println("Hello World on " + Thread.currentThread());
     }
 };
 SwingUtilities.invokeAndWait(doHelloWorld);
 System.out.println("Waiting ... ");
 
If invokeAndWait is called from the event dispatching thread, e.g. from a JButtons ActionListener, the doRun.run() will still be deferred till all pending events have been processed. Note that if the doRun.run() throws an uncaught exception the event dispatching thread will unwind (not the current thread).

Additional documentation and examples for this method can be found in .

See Also:
invokeAndWait
 o invokeAndWait
 public static void invokeAndWait(Runnable doRun) throws InterruptedException, InvocationTargetException
Causes doRun.run() to be executed synchronously on the AWT event dispatching thread. This call will block until all pending AWT events have been processed and (then) doRun.run() returns. This method should be used when an application thread needs to update the GUI. It should not be called from the EventDispatchThread. Here's an example that creates a new application thread that uses invokeAndWait() to print a string from the event dispatching thread and then, when that's finished, print a string from the application thread.
 final Runnable doHelloWorld = new Runnable() {
     public void run() {
         System.out.println("Hello World on " + Thread.currentThread());
     }
 };
 Thread appThread = new Thread() {
     public void run() {
         try {
             SwingUtilities.invokeAndWait(doHelloWorld);
         }
         catch (Exception e) {
		   e.printStackTrace();
         }
         System.out.println("Finished on " + Thread.currentThread());
     }
 };
 appThread.start();
 
Note that if the Runnable.run() method throws an uncaught exception (on the event dispatching thread) it's caught and rethrown, as an InvocationTargetException, on the callers thread.

Additional documentation and examples for this method can be found in .

Throws: InterruptedException
If we're interrupted while waiting for the event dispatching thread to finish excecuting doRun.run()
Throws: InvocationTargetException
If doRun.run() throws
See Also:
invokeLater
 o isEventDispatchThread
 public static boolean isEventDispatchThread()
Returns:
true if the current thread is an AWT event dispatching thread.
 o getAccessibleIndexInParent
 public static int getAccessibleIndexInParent(Component c)
Get the index of this object in its accessible parent.

Returns:
-1 of this object does not have an accessible parent. Otherwise, the index of the child in its accessible parent.
 o getAccessibleAt
 public static Accessible getAccessibleAt(Component c,
                                          Point p)
Returns the Accessible child contained at the local coordinate Point, if one exists.

Returns:
the Accessible at the specified location, if it exists
 o getAccessibleStateSet
 public static AccessibleStateSet getAccessibleStateSet(Component c)
Get the state of this object.

Returns:
an instance of AccessibleStateSet containing the current state set of the object
See Also:
AccessibleState
 o getAccessibleChildrenCount
 public static int getAccessibleChildrenCount(Component c)
Returns the number of accessible children in the object. If all of the children of this object implement Accessible, than this method should return the number of children of this object.

Returns:
the number of accessible children in the object.
 o getAccessibleChild
 public static Accessible getAccessibleChild(Component c,
                                             int i)
Return the nth Accessible child of the object.

Parameters:
i - zero-based index of child
Returns:
the nth Accessible child of the object
 o findFocusOwner
 public static Component findFocusOwner(Component c)
Return the child component which has focus, if any. The HotJava SecurityManager forbids applet access to getFocusOwner(), so if the component is an applet, we check whether a JComponent has focus. Non-Swing components in an applet on HotJava are out-of-luck, unfortunately.

 o getRootPane
 public static JRootPane getRootPane(Component c)
If c is a JRootPane descendant return its JRootPane ancestor. If c is a RootPaneContainer then return its JRootPane.

Returns:
the JRootPane for Component c or null.
 o getRoot
 public static Component getRoot(Component c)
Returns:
the first ancestor of c that's a Window or the last Applet ancestor.

All Packages  Class Hierarchy  This Package  Previous  Next  Index